package com.zsc.newbuds.service.impl;

import com.zsc.newbuds.consts.Consts;
import com.zsc.newbuds.entity.*;
import com.zsc.newbuds.mapper.RecordMapper;
import com.zsc.newbuds.mapper.RecycleMapper;
import com.zsc.newbuds.mapper.SaleRecordMapper;
import com.zsc.newbuds.mapper.UserMapper;
import com.zsc.newbuds.service.RecordService;
import com.zsc.newbuds.utils.AesUtil;
import com.zsc.newbuds.utils.QRCodeUtil;
import com.zsc.newbuds.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

/**
 * @author D
 */
@Service
public class RecordServiceImpl implements RecordService {


    @Autowired
    RedisUtil redisUtil;

    @Autowired
    QRCodeUtil qrCodeUtil;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RecycleMapper recycleMapper;

    @Resource
    private RecordMapper recordMapper;

    @Resource
    private SaleRecordMapper saleRecordMapper;

    @Autowired
    AesUtil aesUtil;

    @Override
    public List<ImageData> addRecord(Integer recycleId, int clothesNumber, int booksNumber) {

        String userKey = "list:userList";
        String recordKey = "list:recordList";
        Record record = new Record();
        String key = "list:recycleList";
        Recycle recycle = recycleMapper.getRecycleById(recycleId);
        //查询用户信息
        User user = userMapper.getUserById(recycle.getUserId());
        System.out.println(user);
        //图片在OSS中的地址
        String imgUrl = "";
        record.setDonor(user.getRealName());
        record.setStartAddress(recycle.getRecycleArea());
        record.setRecycleId(recycleId);
        record.setUserId(recycle.getUserId());
        record.setState("未处理");
        record.setJsonAddress("暂无");
        record.setDonationArea("暂未分配");
        record.setOperator("暂未处理");

        List<ImageData> list = new ArrayList<ImageData>();
        //图书为0则不打印图书的二维码
        if(booksNumber!=0){
            record.setDonationName("书籍");
            recordMapper.addRecord(record);
            String json = aesUtil.base64encode(record.toJson().getBytes());
            imgUrl = qrCodeUtil.encode(json,null,record.getRecordId().toString(),true);

            //设置表格属性
            generateExcel(list, record, imgUrl);
            if(redisUtil.hasKey(recordKey)){
                redisUtil.hset(recordKey,record.getRecordId().toString(),record);
            }
        }
        if(clothesNumber!=0){
            record.setDonationName("衣服");
            for(int i = 0; i<clothesNumber;i++){
                recordMapper.addRecord(record);
                System.out.println(record+"衣服");
                String json = aesUtil.base64encode(record.toJson().getBytes());
                imgUrl = qrCodeUtil.encode(json,null,record.getRecordId().toString(),true);
                generateExcel(list, record, imgUrl);
                if(redisUtil.hasKey(recordKey)){
                    redisUtil.hset(recordKey,record.getRecordId().toString(),record);
                }
            }
        }
        System.out.println(list);
        return list;
    }

    @Override
    public List<Record> getRecordList() {
        List<Record> records = new ArrayList<Record>();
        records = recordMapper.getRecordList();
//        String key = "list:recordList";
//        if(redisUtil.hasKey(key)){
//            System.out.println("从redis中获取recordList");
//            Map<Object, Object> map = redisUtil.hmget(key);
//            for(Map.Entry<Object, Object> record:map.entrySet()){
//                records.add((Record) record.getValue());
//            }
//        }else {
//            System.out.println("从数据库中获取recordList");
//            Map< String,Object> map = new HashMap<>();
//            records = recordMapper.getRecordList();
//            System.out.println(records);
//            for (Record record : records){
//                map.put(record.getRecordId().toString(),record);
//            }
//            redisUtil.hmset(key,map);
//        }
        return records;
    }

    @Override
    public void setDistributionRecord(Integer[] recordId) {
        String key = "list:recordList";
        String distributionKey = "list:distributionKey";
        for (Integer id: recordId){
            Record record = recordMapper.getRecordById(id);
            redisUtil.hset(distributionKey,id.toString(),record);
        }
    }

    @Override
    public List<Record> getDistributionRecord() {
        //List<Record> records = new ArrayList<> ();
//        String key = "list:distributionKey";
//        Map<Object, Object> map = redisUtil.hmget(key);
//        for(Map.Entry<Object, Object> record:map.entrySet()){
//            records.add((Record) record.getValue());
//        }
        List<Record> records = recordMapper.getRecordByState();
        return records;
    }

    @Override
    public void allocateDonationArea(String area, Integer[] recordId) {
        String distributionKey = "list:distributionKey";
        String key = "list:recordList";
        String donateKey = "list:donateList";
        for (Integer id: recordId){
            Record record = recordMapper.getRecordById(id);
            record.setDonationArea(area);
            redisUtil.hset(donateKey,id.toString(),record);
            redisUtil.hdel(key,id.toString());
        }
        //删除缓存
        redisUtil.del(distributionKey);
        //让数据库慢慢存入
        for (Integer id: recordId){
            recordMapper.addDonationArea(id,area);
        }
    }

    @Override
    public List<Record> getSaleList() {
        // 这里有东西
        // TODO: 2021/4/5
        List<Record> recordList = new ArrayList<>();

        List<Integer> idList = Consts.recordIdList;
        for (Integer id : idList) {
            Record record = recordMapper.getRecordById(id);
            recordList.add(record);
        }
        return recordList;
    }

    @Override
    public List<Record> getAimRecordList(String[] recordId) {
        List<Record> records = new ArrayList<>();
//        String soldKey = "list:soldList";
//        for (String id: recordId){
//            records.add((Record) redisUtil.hget(soldKey,id));
//        }
        for (String id : recordId) {
            Record record = recordMapper.getRecordById(Integer.parseInt(id));
            records.add(record);
        }
        return records;
    }

    @Override
    public List<Record> getSoldList() {
        List<Record> SaleRecords = new ArrayList<>();
        //SaleRecords = saleRecordMapper.getSaleRecordList();
        String key = "list:soldList";
        if(redisUtil.hasKey(key)){
            Map<Object, Object> map = redisUtil.hmget(key);
            for(Map.Entry<Object, Object> record:map.entrySet()){
                SaleRecords.add((Record) record.getValue());
            }
        }
        return SaleRecords;
    }

    @Override
    public List<Record> getDonateList() {
        List<Record> records = new ArrayList<>();
        String key = "list:donateList";
        if(redisUtil.hasKey(key)){
            Map<Object, Object> map = redisUtil.hmget(key);
            for(Map.Entry<Object, Object> record:map.entrySet()){
                records.add((Record) record.getValue());
            }
        }
        return records;
    }

    @Override
    public void generateExcel(List<ImageData> list, Record record, String imageUrl) {
        ImageData imageData = new ImageData();

        imageData.setDonor(record.getDonor());
        imageData.setDonationName(record.getDonationName());
        imageData.setAddress(record.getStartAddress());
        imageData.setRecordId(record.getRecordId());
        imageData.setRecycleId(record.getRecycleId());
        imageData.setTime(new Date());
        try {
            imageData.setUrl(new URL(imageUrl));
            list.add(imageData);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
